package com.example.demo.advice;

import com.example.demo.domain.ResponseBody;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    public ResponseEntity<ResponseBody<Object>> handleGlobalException(Exception ex) {
        ResponseBody<Object> responseBody = new ResponseBody<>();
        responseBody.setData(null);
        responseBody.setSuccess(false);
        responseBody.setMessage(ex.getMessage());
        // 将响应数据写入响应体
        log.error(ex.getMessage(), ex);
        return new ResponseEntity<>(responseBody, HttpStatus.BAD_REQUEST);
    }

}
